<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	
	/**
     * @var Zend_Application_Module_Autoloader
     */
    protected $_resourceLoader;
    
    protected $_frontController;
	    
    /**
     * Configure the default modules autoloading, here we first create
     * a new module autoloader specifiying the base path and namespace
     * for our default module. This will automatically add the default
     * resource types for us. We also add two custom resources for Services
     * and Model Resources.
     */
    protected function _initDefaultModuleAutoloader()
    {
        //$this->_logger->info('Bootstrap ' . __METHOD__);
        
        $this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Buyback',
            'basePath'  => APPLICATION_PATH . '/modules/buyback',
        ));
        $this->_resourceLoader->addResourceTypes(array(
            'modelResource' => array(
              	'path'      => 'models/resources',
             	'namespace' => 'Resource',
            ),
            'helper'		=> array(
            	'path'		=> 'helpers',
            	'namespace'	=> 'Helper',
            )
        ));
    }
    
    protected function _initPackingModuleAutoloader()
    {
        //$this->_logger->info('Bootstrap ' . __METHOD__);
        
        $this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Packing',
            'basePath'  => APPLICATION_PATH . '/modules/packing',
        ));
        /*$this->_resourceLoader->addResourceTypes(array(
            'modelResource' => array(
              	'path'      => 'models/resources',
             	'namespace' => 'Resource',
            ),
            'helper'		=> array(
            	'path'		=> 'helpers',
            	'namespace'	=> 'Helper',
            )
        ));*/
    }
    
	protected function _initAdminModuleAutoload(){
    	
    	$this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
    			'namespace'	=>	'Admin',
    			'basePath'	=>	APPLICATION_PATH . '/modules/admin',
    	));
    	$this->_resourceLoader->addResourceTypes(array(
            'helper'		=> array(
            	'path'		=> 'helpers',
            	'namespace'	=> 'Helper',
            )
        ));
    	
    }
    
    protected function _initDoctrineModuleAutoload()
    {
        $this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Doctrine',
            'basePath'  => APPLICATION_PATH . '/modules/doctrine',
        ));
        
        $this->_resourceLoader->addResourceTypes(array(
        	'modelBase'	=>	array(
        		'path'		=>	'models/Base',
        		'namespace'	=>	'Base'
        	)
        ));
    }
    
    protected function _initUserModuleAutoload(){
    	
    	$this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
    			'namespace'	=>	'User',
    			'basePath'	=>	APPLICATION_PATH . '/modules/user',
    	));
    	$this->_resourceLoader->addResourceTypes(array(
            'helper'		=> array(
            	'path'		=> 'helpers',
            	'namespace'	=> 'Helper',
            )
        ));
    }
    
	protected function _initOrderModuleAutoload(){
    	
    	$this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
    			'namespace'	=>	'Order',
    			'basePath'	=>	APPLICATION_PATH . '/modules/order',
    	));
    	
    	$this->_resourceLoader->addResourceTypes(array(
    			'modelResource'	=> array(
    				'path'		=>	'models/resources',
    				'namespace'	=>	'Resource'
    			),
	    		'helper'		=> array(
	            	'path'		=> 'helpers',
	            	'namespace'	=> 'Helper',
            	)
    	));
    }
    
    protected function _initControllerPlugins()
	{
		$frontController = Zend_Controller_Front::getInstance();
		$frontController->registerPlugin(new Access_Controller_Plugin_Authenticator());
		$frontController->registerPlugin(new Custom_Controller_Plugin_LayoutManager());
	}
	
	protected function _initSearchHelper(){
		Zend_Controller_Action_HelperBroker::addHelper(
            new Buyback_Helper_Search()
        );
	}
	
	protected function _initAdminOrderHelper(){
		Zend_Controller_Action_HelperBroker::addHelper(
            new Admin_Helper_OrderAdjustment()
        );
        Zend_Controller_Action_HelperBroker::addHelper(
            new Admin_Helper_ItemAdjustment()
        );
        Zend_Controller_Action_HelperBroker::addHelper(
            new Admin_Helper_ItemRejection()
        );
        Zend_Controller_Action_HelperBroker::addHelper(
            new Admin_Helper_StatusAdjustment()
        );
        Zend_Controller_Action_HelperBroker::addHelper(
            new Admin_Helper_ItemCondition()
        );
	}
	
	/*protected function _initPackingSlipHelper(){
		Zend_Controller_Action_HelperBroker::addHelper(
			new Packing_Helper_UPS()
		);
		Zend_Controller_Action_HelperBroker::addHelper(
			new Packing_Helper_USPS()
		);
	}*/
	
	/**
     * Add the config to the registry
     */
    protected function _initConfig()
    {
        //$this->_logger->info('Bootstrap ' . __METHOD__);
        Zend_Registry::set('config', $this->getOptions());
    }

    protected function _initDoctrine()
    {
    	
    	require_once(APPLICATION_PATH . '/../library/Doctrine.php');
    	require_once(APPLICATION_PATH . '/../library/Doctrine.php');
        spl_autoload_register(array('Doctrine_Core','autoload'));
        spl_autoload_register(array('Doctrine_Core', 'modelsAutoload'));

        $manager = Doctrine_Manager::getInstance();
        $manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
        $manager->setAttribute(
            Doctrine_Core::ATTR_MODEL_LOADING, 
            Doctrine_Core::MODEL_LOADING_AGGRESSIVE
        );
        $manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);

		$doctrineConfig = $this->getOption("doctrine");
        $conn = Doctrine_Manager::connection($doctrineConfig['dsn'], 'doctrine');
        $conn->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);
        
        return $conn;
    }
    
	protected function _initNavigation(){
		$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navMenu.xml');
		$container = new Zend_Navigation($config);
		$registry = Zend_Registry::getInstance();
		$registry->set('Zend_Navigation', $container);
	}
}